package com.sxt.server.util;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.Map;

/**
 * RestClient is used to http请求
 */
@Component
public class RestClient {

//    @Resource
//    public RestTemplate restTemplate;
	
	  public RestTemplate restTemplate(){
		  	SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
	        factory.setReadTimeout(5000);//单位为ms
	        factory.setConnectTimeout(5000);//单位为ms
	        return new RestTemplate(factory);
	    }
	 
    
    /**
     * get
     * @param url 请求地址
     * @param param  参数
     * @param returnClass 返回类型
     * @return
     */
    public <T> T get(String url, Class<T> returnClass, Map<String, ?> param) {
        return restTemplate().getForObject(url, returnClass, param);
    }
    
    /**
     * post
     * @param url 请求地址
     * @param param 参数
     * @param returnClass 返回类型
     * @param header 自定义的头信息
     * @return
     */
    public <E> E post(String url, E param, Class<E> returnClass, Map<String, String> header) {
        HttpHeaders headers = new HttpHeaders();
        header.forEach((o1, o2) -> headers.set(o1, o2));
        HttpEntity<E> httpEntity = new HttpEntity<E>(param,headers);
        return restTemplate().postForObject(url, httpEntity, returnClass);
    }
    
    /**
     * post
     * @param url 请求地址
     * @param param 参数
     * @param returnClass 返回类型
     * @return
     */
    public <E> E postByDefault(String url, E param, Class<E> returnClass) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Accept", "application/json");
        HttpEntity<E> httpEntity = new HttpEntity<E>(param,headers);
        return restTemplate().postForObject(url, httpEntity, returnClass);
    }
}
